package com.liming.practice;

import java.util.HashMap;
import java.util.Map;

public class MapPractice {
    public static void main(String[] args) {
        Map<String,String> data = new HashMap<>();
        data.put("小红","A");
        data.put("小黄","B");
        data.put("小蓝","C");
        data.put("小绿","D");
        data.put("小紫","A");
        data.put("小粉","C");
        data.put("小黑","D");
        count(data);

    }
    public static void count(Map<String,String> data){
        //统计投票结果
        //key: 投票人
        //value: 投票结果
        //遍历map集合，获取每个投票人和投票结果
        //如果map集合中已经存在该投票结果，将该投票结果的值加1
        Map<String,Integer> sum = new HashMap<>();
        data.forEach((key,value) -> {
            if(sum.containsKey(value)){
                Integer oldCount = sum.get(value);
                sum.put(value,oldCount + 1);
            }else{
                sum.put(value,1);
            }
        });
        //遍历sum集合，输出每个投票结果和对应的人数
        sum.forEach((key,value) -> System.out.println(key + " : " + value));

    }


}
